package com.icode.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.icode.gulimall.product.entity.AttrEntity;
import com.icode.gulimall.product.service.AttrAttrgroupRelationService;
import com.icode.gulimall.product.service.AttrService;
import com.icode.gulimall.product.service.CategoryService;
import com.icode.gulimall.product.vo.AttrGroupRelationVo;
import com.icode.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.icode.gulimall.product.entity.AttrGroupEntity;
import com.icode.gulimall.product.service.AttrGroupService;
import com.icode.common.utils.PageUtils;
import com.icode.common.utils.R;



/**
 * 属性分组
 *
 * @author my_code
 * @email 1345743081@gmail.com
 * @date 2020-10-27 19:38:13
 * /product/attrgroup/{attrgroupId}/attr/relation
 * /product/attrgroup/attr/relation/delete
 * /product/attrgroup/attr/relation
 * /product/attrgroup/{attrgroupId}/noattr/relation
 * /product/attrgroup/attr/relation
 * /product/attrgroup/{catelogId}/withattr
 *
 *
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    AttrGroupService attrGroupService;

    @Autowired
    AttrService attrService;

    @Autowired
    CategoryService categoryService;

    @Autowired
    AttrAttrgroupRelationService relationService;

    @GetMapping("/{catelogId}/withattr")
    public R withAttr(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrsVo> data = attrGroupService.getAllAttrs(catelogId);
        return R.ok().put("data", data);
    }

    /**
     * 添加属性与分组关联关系
     */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody AttrGroupRelationVo[] relationVos){
        relationService.saveRelation(relationVos);
        return R.ok();
    }


    /**
     * 获取属性分组的关联的所有属性
     * @param attrgroupId
     * @return
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> data = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data", data);
    }

    /**
     *  获取属性分组没有关联的其他属性
     * @param attrgroupId
     * @return
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params){
       PageUtils page = attrService.attrNoRelation(params, attrgroupId);
        return R.ok().put("page", page);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     *  获取分类属性分组
     */
    @RequestMapping("/list/{catelogId}")
    public R listCatelogId(@RequestParam Map<String, Object> params,
                           @PathVariable("catelogId") Long catelogId){
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catalogId = attrGroup.getCatelogId();
        Long[] path = categoryService.findCatalogPath(catalogId);
        attrGroup.setCatelogPath(path);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * (批量)删除
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] relationVos){
        attrService.deleteRelation(relationVos);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
